package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.databinding.ProfitRecordItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

/**
 * Created by guluwa on 2018/11/23.
 */

class ProfitRecordListAdapter(val list: MutableList<Any>,
                              private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: List<Any>, page: Int) {
        if (page == 2) list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.profit_record_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            /*val recordsBean = list[position] as ProfitRecordsBean.RecordsBean
            (holder as ViewHolder).databinding.tvProfitRecordFrom.text =recordsBean.nickName //String.format("%s%s", recordsBean.title, if (recordsBean.logno != "") recordsBean.logno else "")
            holder.databinding.tvProfitRecordDate.text = recordsBean.createTime
            var type=""
            if ("4"==recordsBean.type){
                type="季度分红"
            }else {
                type = if (1 == recordsBean.moneytype) {
                    "预估"
                } else {
                    "结算"
                }
                var type1=""
                when(recordsBean.type){
                    "1"->{type1="自购奖励"}
                    "2"->{type1="团队奖励"}
                    "3"->{type1="平级奖励"}
                    "5"->{type1="推荐奖励"}
                }
                type += type1
            }
            holder.databinding.tvtype.text = type
            holder.databinding.tvProfitRecordMoney.text ="￥${recordsBean.credit2}"
            Glide.with(holder.itemView).asBitmap()
                    .load(Contacts.REMOTE_IMAGE_URL + recordsBean.headPic)
                    .apply(RequestOptions().circleCrop().error(R.drawable.ic_tribe_boy_thumb))
                    .into(holder.databinding.userimag)*/
        }
    }

    inner class ViewHolder(val databinding: ProfitRecordItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }
}